Release 10.1A: OpenEdge Development:
Programming Interfaces
Results lists
A results list is a list of
ROWIDs that satisfy a query. The results list allows you to quickly access the records in the record set you define, and allows Progress to make the records available one at a time, as needed.When more than one row satisfies a query, Progress doesn’t lock all of the records and hold them until you release them. Instead, when a specific record is needed, Progress uses the record’s ID to go directly to the record, locking only that record. By going directly to the record, Progress also ensures that you have the latest copy of the record.
When you open a query, Progress does not normally build the entire results list. Instead it initializes the results list and adds to it as needed. The
NUM–RESULTSfunction returns the number of records currently in the results list. This is not necessarily the total number of records that satisfy the query.Whether and when Progress builds the results list depends on the type of the query. As shown in Table 1–3, the
DOorREPEATPRESELECTstatements always use a results list, while theFOREACHandOPENQUERYstatements sometimes use a results list.Queries have the following characteristics:
- Scrolling versus non-scrolling — A query is scrolling if you specify
SCROLLINGin theDEFINEQUERYstatement or if you define a browse for the query. You can use theREPOSITIONstatement to change your current position within the results list. For a non-scrolling query, you can only move sequentially through the rows by using theFIRST,LAST,NEXT, andPREVoptions of theGETstatement. Scrolling queries must use a results list (often initially empty); non-scrolling queries might not.- Index-sorted — If the order of the rows in the query can be determined by using a single index, the query is index-sorted. For an index-sorted query, Progress can use the index to order records without a results list. However, if the query requires additional sorting it must also be presorted.
- Presorted — If the query requires any sorting without an index or with multiple indexes, it must be presorted. For a presorted query, Progress must read all the records, sort them, and build the complete results list before any records are fetched.
- Preselected versus non-preselected — You can force Progress to build a preselected results list by specifying
PRESELECTon theOPENQUERY,DO, orREPEATstatement.Table 1–4 summarizes how the results list is built for each type of query.
Table 1–4: Results lists for specific query types Query type Results list Non-scrolling, index-sorted, no preselection None. Scrolling, no sorting, no preselection Empty list1 established when query is opened. Records are added to the results list as needed. Presorted or preselected Complete list built when query is opened.
1If a browse is defined for the query, the results list initially contains one row.
There are two cases where Progress has to build the entire results list when you first open the query:
- When you have explicitly used the
PRESELECToption. In this case, Progress performs a preselection phase in which it reads each record of the query. You can specify the lock type to use during the preselection phase. (For anOPENQUERY, the lock type you specify in theOPENQUERYstatement is used for the preselection phase.) These locks are released immediately unless the preselection occurs within a transaction.- When you have not used the
PRESELECToption, but have specified sort criteria that cannot be performed using an index. In this case, Progress performs a presort phase in which it reads each record of the query withNO–LOCKand builds the results list.For example, the following statement explicitly uses the
PRESELECToption. This forces Progress to build the entire results list immediately:
If you had used
FORinstead ofPRESELECT, Progress would not have had to build the entire results list because it uses the primary index to fetch the records. It could use this index to find the first or last record for the query; it only needs to search forward or backward through the index until it finds a record that satisfies theWHEREclause.You can use the
PRESELECToption of theOPENQUERYstatement when you need to know immediately how many records satisfy the query or you can use it to immediately lock all the records that satisfy the query.Progress also builds a complete results list when you open a query with a sort condition that cannot be resolved using a single index. Suppose you open a query on the customer table as follows:
Because there is no index for the city field, Progress must retrieve all the records that satisfy the query (in this case, all the customer records), perform the sort, and build the entire results list before any records can be fetched. Until it performs this sort, Progress cannot determine the first or last record for the query. If an index were defined on the city field, Progress could use that index to fetch the records in sorted order (forwards or backwards) and would not need to build the results list in advance.
If the sort conditions for a query can be resolved using a single index, you can use the
GETstatement with theFIRST,LAST,NEXT, andPREVoptions on that query. For example, the following query is sorted using the primary index:
Because the sorting is done with a single index, you can move freely forwards and backwards within the query.
Note: If you want to use theREPOSITIONstatement on a query, you must make the query scrolling by specifying theSCROLLINGoption in aDEFINEQUERYstatement.Navigating a Results list
As shown in Table 1–3, results lists are associated with the
OPENQUERYandGETstatements. However, Progress only guarantees a results list if you first define the query with theSCROLLINGoption:
This option indicates to Progress that you want to use the results list for multi-directional navigation.
You can use the
REPOSITIONstatement to specify how many places forward or backward you want to move, so that you can skip over a given number records. It also allows you to move to a specificROWID.The
REPOSITIONstatement changes your location in the results list but does not actually fetch the record (unless the query is associated with a browse widget). To actually fetch records in a results list, you use theGETstatement. The following example illustrates how theREPOSITIONstatement works:
After a record is fetched (with a
GETstatement), the results list position is on theROWID, so thatGETNEXTgets the next record, andGETPREVgets the previous record. After aREPOSITION, the position is always between two records. Thus,REPOSITIONFORWARD 0repositions the results list immediately after the current record.GETNEXTfetches the next record;GETPREVfetches the previous record.REPOSITIONFORWARD 1repositions the results list between the next record and the record after it.To find the total number of rows in a results list, you can use the
NUM–RESULTSfunction. To find the current position within a results list, you can use theCURRENT–RESULT–ROWfunction.As Table 1–3 shows, Progress also creates results lists for
FOREACHstatements and forDOandREPEATstatements with thePRESELECTphrase. However, you cannot navigate freely through a results list created for theFOREACHstatement. If the results list was created for theFOREACHstatement, then Progress automatically steps through the results list in sorted order:
Within a
PRESELECTblock, you can use theFINDstatement to move backwards and forwards through the results list:
Note: A powerful way of navigating a record set is with the browse widget. For more information, see OpenEdge Development: Progress 4GL Handbook .
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |